package com.eralink.unionuhfreader.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bumptech.glide.Glide;
import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.ProductVo;

import java.util.List;

public class ManualProductRecycleViewAdapter extends RecyclerView.Adapter<ManualProductRecycleViewAdapter.ViewHold> {

    private List<ProductVo> mList;//数据源
    private Context mContext;

    private Itemclick itemclick;

    public void setItemclick(Itemclick itemclick) {
        this.itemclick = itemclick;
    }

    public ManualProductRecycleViewAdapter(Context context, List<ProductVo> vos) {
        this.mContext = context;
        this.mList = vos;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_manuproduct, parent, false);
        //将view传递给我们自定义的ViewHolder
        ViewHold holder = new ViewHold(view);
        //返回这个MyHolder实体
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, @SuppressLint("RecyclerView") int position) {
        if (mList.get(position).getImgUrl() != null && !mList.get(position).getImgUrl().equals("")) {
            try{
                JSONArray imgs= JSON.parseArray(mList.get(position).getImgUrl());
                if (imgs.size()>0){
                    Glide.with(mContext).load(imgs.get(0).toString()).placeholder(mContext.getResources().getDrawable(R.mipmap.logo)).into(holder.img_product);
                }else{
                    Glide.with(mContext).load(mContext.getResources().getDrawable(R.mipmap.logo));
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            Glide.with(mContext).load(mContext.getResources().getDrawable(R.mipmap.logo));
        }

        holder.productname.setText(mList.get(position).getProductName());
        holder.price.setText("价格:"+mList.get(position).getSalePrice() + "");
        holder.inv.setText("库存:"+mList.get(position).getInventory() + "");
        holder.ly_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemclick!=null){
                    itemclick.itemonclick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHold extends RecyclerView.ViewHolder {
        private TextView productname;
        private TextView inv, price;
        private ImageView img_product;
        private LinearLayout ly_item;
        public ViewHold(View convertView) {
            super(convertView);
            productname = (TextView) convertView.findViewById(R.id.productname);
            inv = (TextView) convertView.findViewById(R.id.inv);
            price = (TextView) convertView.findViewById(R.id.price);
            img_product = (ImageView) convertView.findViewById(R.id.img_product);
            ly_item=convertView.findViewById(R.id.ly_item);
        }
    }

   public interface Itemclick{
        void itemonclick(int postion);
    }
}
